﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace YourLastOptionsDialog.Common
{
    /// <summary>
    /// A highly specialized converter just for YLOD. Takes a parameter in the format X|Y, where X and Y are doubles.
    /// If the given value is null or empty or contains only white spaces, then a margin of (0, X, 0, 0) is returned;
    /// otherwise, a margin of (0, Y, 0, 0) is returned.
    /// </summary>
    public class StringToTopMarginConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        [SuppressMessage("Microsoft.Usage", 
            "CA1806:DoNotIgnoreMethodResults", 
            MessageId = "System.Double.TryParse(System.String,System.Double@)", 
            Justification = "The default value is expected when the conversion fails.")]
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            var text = value as string;
            var margins = parameter.ToString().Split('|');
            if (margins.Length != 2)
            {
                return null;
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                var topMargin = 0.0;
                double.TryParse(margins[0], out topMargin);
                return new Thickness(0.0, topMargin, 0.0, 0.0);
            }
            else
            {
                var topMargin = 0.0;
                double.TryParse(margins[1], out topMargin);
                return new Thickness(0.0, topMargin, 0.0, 0.0);
            }
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}